var grid;
function initGrid(staffRoleId){
	$("#startDate").ligerDateEditor();
	$("#endDate").ligerDateEditor();
	grid = $("#maingrid").ligerGrid({
		url:HTR.U('index/orders/pageQuery'),
		pageSize:HTR.pageSize,
		pageSizeOptions:HTR.pageSizeOptions,
		height:'99%',
        width:'100%',
        minColToggle:6,
        rownumbers:true,
        columns: [
	        { display: '姓名', name: 'user_name',isSort: false,width:80},
	        { display: '产品名称', name: 'goodsName',align:'left',isSort: false},
	        { display: '产品单价', name: 'salePrice',isSort: false,width:80},
	        { display: '数量', name: 'saleNum',isSort: false,width:80},
	        { display: '区域', name: 'areaPathName',isSort: false,width:200},
	        { display: '消费时间', name: 'createTime',isSort: false,width:100},
	        { display: '业务员', name: 'staffName',isSort: false,width:100},
	        { display: '疾病类型', name: 'diseaseName',isSort: false,width:100},
	        { display: '订单总额', name: 'total',isSort: false,width:80},
	        { display: '操作', name: 'op',isSort: false,width:100,render: function (rowdata, rowindex, value){
	            var h = "";
	            if(HTR.GRANT.KHDD_02 && staffRoleId < 4)h += "<a href='javascript:toEdit(" + rowdata['orderId'] + ")'>编辑</a> ";
	            if(HTR.GRANT.KHDD_03 && staffRoleId < 4)h += "<a href='javascript:del(" + rowdata['orderId'] + ")'>删除</a> "; 
	            return h;
	        }}
        ]
    });
}
function loadGrid(){
	var params = HTR.getParams('.j-ipt');
	params.areaIdPath = HTR.ITGetAllAreaVals('areaId1','j-areas').join('_');
	grid.set('url',HTR.U('index/orders/pageQuery',params));
}

function del(id){
	var box = HTR.confirm({content:"您确定要删除该订单吗?",yes:function(){
   var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
   $.post(HTR.U('index/orders/del'),{id:id},function(data,textStatus){
   			layer.close(loading);
   			var json = HTR.toIndexJson(data);
   			if(json.status=='1'){
   			    HTR.msg(json.msg,{icon:1});
   			    layer.close(box);
   			    grid.reload();
   			}else{
   			    HTR.msg(json.msg,{icon:2});
   			}
   		});
    }});
}
function toEdit(id){
	location.href=HTR.U('index/orders/toEdit','id='+id);
}
function save(){
	$('#editFrom').isValid(function(v){
		if(v){
			var params = HTR.getParams('.ipt');
			var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
		    $.post(HTR.U('index/orders/'+((params.orderId==0)?"add":"edit")),params,function(data,textStatus){
		    	layer.close(loading);
		    	var json = HTR.toIndexJson(data);
		    	if(json.status=='1'){
		    		HTR.msg("操作成功",{icon:1});
		    		location.href=HTR.U('index/orders/index');
		    	}else{
		    		HTR.msg(json.msg,{icon:2});
		    	}
		    });
		}
	});
}